package com.ropeok.service.impl;

import com.ropeok.dao.UserDao;
import com.ropeok.entity.User;
import com.ropeok.service.UserRoleService;
import com.ropeok.service.UserService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: task2
 * @BelongsPackage: com.ropeok.service
 * @Author: zhoufeifan
 * @CreateTime: 2022-08-23  14:24
 * @Description: TODO
 * @Version: 1.0
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    private static Logger logger = LoggerFactory.getLogger(UserService.class);
    @Autowired
    private UserDao userDao;
    /**
     * @description: 根据ID查询用户
     * @author: zhoufeifan
     * @date: 2022/9/15 10:34
     * @param: [long]
     * @return: com.ropeok.entity.User
    **/
    @Override
    public User findById(long id) {
        User user = userDao.getUserById(id);
        return user;
    }
    /**
     * @description: 查询角色列表
     * @author: zhoufeifan
     * @date: 2022/9/15 10:34
     * @param: []
     * @return: java.util.List<com.ropeok.entity.User>
    **/
    @Override
    public List<User> getUserList() {
        List<User> userList = userDao.getUserList();
        return userList;
    }
    /**
     * @description: 增加角色记录
     * @author: zhoufeifan
     * @date: 2022/9/15 10:34
     * @param: [com.ropeok.entity.User]
     * @return: int
    **/
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int addUser(User user) throws RuntimeException {

        return userDao.addUser(user);

    }
    /**
     * @description: 删除角色记录
     * @author: zhoufeifan
     * @date: 2022/9/15 10:35
     * @param: [long]
     * @return: int
    **/
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int deleteUser(long id) throws RuntimeException {
        return userDao.deleteUser(id);
    }
    /**
     * @description: 更新角色记录
     * @author: zhoufeifan
     * @date: 2022/9/15 10:35
     * @param: [com.ropeok.entity.User]
     * @return: int
    **/
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updateUser(User user) throws RuntimeException {

        return userDao.updateUser(user);

    }

    /**
     * @description:测试事务
     * @author: zhoufeifan
     * @date: 2022/9/14 11:43
     * @param: []
     * @return: void
     **/
    @Override
    @Transactional
    public void test() {
        for (int i = 11; i < 99; i++) {
            userDao.addUser(new User(i, "测试人员" + i, 0, "上海", new Date()));
        }

/*        userDao.addUser(new User(7,"测试人员1",0,"上海",new Date()));
       System.out.println(1/0);
        userDao.addUser(new User(8,"测试人员2",1,"广州",new Date()));*/
    }
    /**
     * @description: 测试事务
     * @author: zhoufeifan
     * @date: 2022/9/15 10:36
     * @param: []
     * @return: void
    **/

    @Override
    public void testTransactional() {
        userDao.addUser(new User("测试人员", 0, "上海", new Date()));


    }

    /**
     * @description:查询某姓的用户
     * @author: zhoufeifan
     * @date: 2022/9/14 13:46
     * @param: []
     * @return: void
     **/
    @Override
    public List<User> getUsersByKeyword() {
        List<User> users = userDao.getUsersByKeyword("测");
        return users;
    }

    /**
     * @description:查询名字中有XX的用户
     * @author: zhoufeifan
     * @date: 2022/9/14 13:47
     * @param: []
     * @return: void
     **/
    @Override
    public List<User> getUsersByMidKeyword() {
        List<User> users = userDao.getUsersByMidKeyword("小");
        return users;
    }
    @Override
    public List<User> getUserByLimit(Integer page,Integer pageSize ) {
         Integer offset = (page-1)*pageSize;
        HashMap<String, Integer> map = new HashMap<>();
        map.put("offset",offset);
        map.put("pageSize",pageSize);
        List<User> userByLimit = userDao.getUserByLimit(map);
        return userByLimit;
    }
}
